﻿using System;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using Excel;

public partial class Default : System.Web.UI.Page
{
    // Author: Otto Neff | webapps-in-action.com
    protected DataSet Data
    {
        get
        {
            if (Application["data"] == null)
            {
                return null;
            }
            return Application["data"] as DataSet;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Data == null)
        {
            FileStream stream = File.Open(Server.MapPath("MyExcelSheet.xlsx"), FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            excelReader.IsFirstRowAsColumnNames = true;
            Application["data"] = excelReader.AsDataSet();
            excelReader.Close();
        }
    }
    protected void radGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        if (Data != null)
        {
            (sender as RadGrid).DataSource = Data;
        }
    }
}
